package cn.yhjz.platform.system.controller;

import cn.yhjz.common.util.FileUtil;
import cn.yhjz.platform.system.entity.SysUser;
import cn.yhjz.platform.system.service.ISysUserAvatarService;
import cn.yhjz.platform.system.entity.SysUserAvatar;
import cn.yhjz.platform.system.util.LoginUtil;
import cn.yhjz.platform.system.vo.SysUserAvatarVo;
import cn.yhjz.common.AjaxResponse;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/system/avatar")
@Slf4j
public class SysUserAvatarController {

    @Autowired
    private ISysUserAvatarService sysUserAvatarService;

    @PostMapping("/list")
    public List<SysUserAvatar> list(@RequestBody SysUserAvatar sysUserAvatar) {
        return sysUserAvatarService.select(sysUserAvatar);
    }

    @PostMapping("/page")
    public AjaxResponse page(@RequestBody SysUserAvatarVo vo) {
        SysUserAvatar entity = new SysUserAvatar();
        BeanUtils.copyProperties(vo, entity);
        return AjaxResponse.success(sysUserAvatarService.getPage(vo.getCurrentPage(), vo.getPageSize(), entity));
    }

    @PostMapping("/addOrUpdate")
    public AjaxResponse add(@RequestBody SysUserAvatar entity) {
        if (entity.getId() != null) {
            return AjaxResponse.success(sysUserAvatarService.updateById(entity));
        } else {
            return AjaxResponse.success(sysUserAvatarService.insert(entity));
        }
    }

    @GetMapping("/selectById")
    public AjaxResponse selectById(Long id) {
        return AjaxResponse.success(sysUserAvatarService.selectById(id));
    }

    @DeleteMapping("/deleteById")
    public AjaxResponse deleteById(Long id) {
        return AjaxResponse.success(sysUserAvatarService.deleteById(id));
    }

    @PostMapping("/upload")
    public AjaxResponse upload(MultipartFile avatar) throws IOException {
        if (null == avatar) {
            return AjaxResponse.fail("no");
        }
        SysUser currentUser = LoginUtil.getCurrentUser();
        SysUserAvatar sysUserAvatar = new SysUserAvatar();
        sysUserAvatar.setFileContent(avatar.getBytes());
        sysUserAvatar.setHashcode(FileUtil.hashFile(avatar.getBytes()));
        sysUserAvatar.setUserCode(currentUser.getUserCode());
        sysUserAvatarService.updateUserAvatar(sysUserAvatar);
        return AjaxResponse.success("success", sysUserAvatar.getHashcode());
    }

    /**
     * 预览当前用户的头像，如果没有头像就返回一个默认的
     */
    @GetMapping(value = "/preview",
            produces = {
                    "image/jpeg",
                    "image/png",
            })
    public byte[] preview(String file, HttpServletResponse httpServletResponse) {
        if (StrUtil.isBlank(file)) {
            return null;
        }
        SysUserAvatar param = new SysUserAvatar();
        param.setHashcode(file);
        SysUserAvatar sysUserAvatar = sysUserAvatarService.selectOne(param);
        //让浏览器缓存头像一小时
        httpServletResponse.setHeader("cache-control", "must-revalidate,max-age=3600");
        return sysUserAvatar.getFileContent();
    }
}
